import React, { useEffect, useState } from 'react';
import ReactECharts from 'echarts-for-react';
import { useWindowSize } from '../hooks/useWindowSize';

const LineChart2: React.FC = () => {
  const { width } = useWindowSize();
  const [fontSize, setFontSize] = useState(16);
  const [chartHeight, setChartHeight] = useState('100%');

  useEffect(() => {
    if (width <= 480) {
      setFontSize(12);
      setChartHeight('200px');
    } else if (width <= 768) {
      setFontSize(14);
      setChartHeight('250px');
    } else if (width <= 992) {
      setFontSize(15);
      setChartHeight('300px');
    } else {
      setFontSize(16);
      setChartHeight('100%');
    }
  }, [width]);

  const option = {
    title: {
      text: '实时监控',
      textStyle: {
        color: '#fff',
        fontSize: fontSize
      }
    },
    tooltip: {
      trigger: 'axis',
      confine: true
    },
    legend: {
      data: ['指标1', '指标2'],
      textStyle: {
        color: '#fff',
        fontSize: fontSize - 2
      },
      top: fontSize + 5
    },
    grid: {
      left: '3%',
      right: '4%',
      bottom: '3%',
      containLabel: true,
      top: fontSize * 2 + 30 // 为图例留出空间
    },
    xAxis: {
      type: 'category',
      boundaryGap: false,
      data: ['00:00', '03:00', '06:00', '09:00', '12:00', '15:00', '18:00', '21:00'],
      axisLine: {
        lineStyle: {
          color: '#fff'
        }
      },
      axisLabel: {
        fontSize: fontSize - 2,
        color: '#fff'
      }
    },
    yAxis: {
      type: 'value',
      axisLine: {
        lineStyle: {
          color: '#fff'
        }
      },
      axisLabel: {
        fontSize: fontSize - 2,
        color: '#fff'
      },
      splitLine: {
        lineStyle: {
          color: 'rgba(255, 255, 255, 0.1)'
        }
      }
    },
    series: [
      {
        name: '指标1',
        type: 'line',
        smooth: true,
        data: [120, 132, 101, 134, 90, 230, 210, 182],
        itemStyle: {
          color: '#91cc75'
        }
      },
      {
        name: '指标2',
        type: 'line',
        smooth: true,
        data: [220, 182, 191, 234, 290, 330, 310, 123],
        itemStyle: {
          color: '#fac858'
        }
      }
    ]
  };

  return (
    <ReactECharts
      option={option}
      style={{ height: chartHeight, width: '100%' }}
      opts={{ renderer: 'svg' }}
      notMerge={true}
      lazyUpdate={true}
    />
  );
};

export default LineChart2; 